first_scan = 1703
last_scan = 1728

import ptycho2 as ptycho
import numpy as np
import ptycho2 as ptycho
from pyE17 import io
import sys




scans = [
#(1810,1872,64.51613,62),
#(1873,1935,64.51613,62),
#(1936,1972,111.11111,36),
#(1973,2009,111.11111,36),
#(2010,2046,111.11111,36),
#(2047,2083,111.11111,36),
#(2084,2120,111.11111,36),
#(2121,2157,111.11111,36),
#(2160,2182,181.81818,22),
#(2183,2205,181.81818,22)]
#(2206,2228,181.81818,22),
#(2229,2251,181.81818,22),
#(2252,2274,181.81818,22),
#(2275,2297,181.81818,22),
#(2298,2320,181.81818,22),
#(2321,2343,181.81818,22),
#(2344,2366,181.81818,22),
#(2367,2389,181.81818,22),
#(2390,2412,181.81818,22),
#(2413,2435,181.81818,22),
#(2438,2452,285.71429,14),
#(2453,2467,285.71429,14),
#(2468,2482,285.71429,14),
#(2483,2497,285.71429,14),
#(2498,2512,285.71429,14),
#(2513,2527,285.71429,14),
#(2528,2542,285.71429,14),
#(2543,2557,285.71429,14),
#(2558,2572,285.71429,14),
#(2573,2587,285.71429,14),
#(2588,2602,285.71429,14),
#(2603,2617,285.71429,14),
#(2618,2632,285.71429,14),
#(2633,2647,285.71429,14),
#(2650,2660,400.00000,10),
#(2661,2671,400.00000,10),
#(2672,2682,400.00000,10),
#(2683,2693,400.00000,10),
#(2694,2704,400.00000,10),
#(2705,2715,400.00000,10),
#(2716,2726,400.00000,10),
#(2727,2737,400.00000,10),
#(2738,2748,400.00000,10),
#(2749,2759,400.00000,10),
#(2760,2770,400.00000,10),
#(2771,2781,400.00000,10),
#(2782,2792,400.00000,10),
#(2793,2803,400.00000,10),
#(2806,2813,571.42857,7),
#(2814,2821,571.42857,7),
#(2822,2829,571.42857,7),
#(2830,2837,571.42857,7),
#(2838,2845,571.42857,7),
#(2846,2853,571.42857,7),
#(2854,2861,571.42857,7),
#(2862,2869,571.42857,7),
#(2870,2877,571.42857,7),
#(2878,2885,571.42857,7),
#(2886,2893,571.42857,7),
#(2894,2901,571.42857,7),
#(2902,2909,571.42857,7),
#(2910,2917,571.42857,7),
#(2920,2926,666.66667,6),
#(2927,2933,666.66667,6),
#(2934,2940,666.66667,6),
#(2941,2947,666.66667,6),
#(2948,2954,666.66667,6),
#(2955,2961,666.66667,6),
#(2962,2968,666.66667,6),
#(2969,2975,666.66667,6),
#(2976,2982,666.66667,6),
#(2983,2989,666.66667,6),
#(2990,2996,666.66667,6),
#(2997,3003,666.66667,6),
#(3004,3010,666.66667,6),
#(3011,3017,666.66667,6),
#(3020,3025,800.00000,5),
#(3026,3031,800.00000,5),
#(3032,3037,800.00000,5),
#(3038,3043,800.00000,5),
#(3044,3049,800.00000,5),
#(3050,3055,800.00000,5),
#(3056,3061,800.00000,5),
#(3062,3067,800.00000,5),
#(3068,3073,800.00000,5),
#(3074,3079,800.00000,5),
#(3080,3085,800.00000,5),
#(3086,3091,800.00000,5),
#(3092,3097,800.00000,5),
#(3098,3103,800.00000,5)]
(8130,8144,285.71429,14),
(8145,8167,181.81818,22),
(8168,8204,111.11111,36),
(8205,8255,80.00000,50),
(8256,8318,64.51613,62),
(8319,8341,181.81818,22),
(8342,8378,111.11111,36),
(8379,8441,64.51613,62),
(8442,8464,181.81818,22),
(8465,8495,133.33333,30),
(8496,8532,111.11111,36),
(8533,8583,80.00000,50),
(8584,8646,64.51613,62),
(8647,8657,400.00000,10),
(8658,8708,80.00000,50),
(8709,8771,64.51613,62),
(8772,8782,400.00000,10),
(8783,8805,181.81818,22),
(8806,8856,80.00000,50),
(8857,8919,64.51613,62)]

def prep_contdmesh_scan(base_path,user,dpsize,ctr,detector_distance,first_scan,last_scan,step_size_y,sx,nx,dat_file):

	# Need to "hack" the defaults for continuous scans
	ptycho.cSAXS.FILENAME_PATTERN = '{read_path}/{prefix}{scan_number:05d}_00000_{index}.{file_extension}'

	# Empty parameter structure
	p = ptycho.Param()

	p.verbose_level = 3
	p.interactive = False                         # Not used for now
	p.base_path = base_path
	p.user = user
	#p.scan_number = 300 
	p.dpsize = dpsize                          # Crop size (used to be "asize")
	p.ctr = ctr                               # Center of cropped array
	#p.motors = ('py', 'px')                      # Motor names in y and x
	#p.motors_multiplier = 1e-3                    # Multiplier to convert motor values to meters
	p.motors = None                               # Motor names in y and x
	p.energy = 6.2                                # Energy in keV
	p.detector_pixel_size = 172e-6                # Detector pixel size
	p.detector_distance = detector_distance                    # Detector distance
	p.scan_type = 'raster'                            # None (use spec), or 'raster', 'round', 'round_ROI'
	p.scan_params.nx = nx
	p.scan_params.ny = 0
	p.scan_params.step_size_x = sx
	p.scan_params.step_size_y = step_size_y
	p.spec_filename = ptycho.cSAXS.spec.SpecInfo(base_path + '/specES1/dat-files/'+dat_file)

#	specinfo = ptycho.cSAXS.spec.SpecInfo(base_path + '/specES1/dat-files/'+dat_file)

	for kk in range(first_scan,last_scan+1):
		p.scan_number = kk
		ptycho.cSAXS.prepare_data(p)

	scan_label = 'S%05d_S%05d' % (first_scan, last_scan)
	
	fpattern = base_path + '/analysis/S%05d/S%05d_data_' + '%dx%d.h5' % dpsize

	# Load all data as dataScan objects
	ds_all = [ptycho.experiment.DataScan(fpattern % (i,i)) for i in range(first_scan, last_scan+1)]

	# The "out" scan will be based on one of these
	ds = ptycho.experiment.DataScan(fpattern % (first_scan,first_scan))

	# Now modify the structure
	# First combine data
	ds.data = np.concatenate([di.data for di in ds_all])
	ds.scan_info.shape = ds.data.shape
	ds.scan_info.scan_label = scan_label

	# Edit positions
	positions = np.concatenate([ds_all[i].scan_info.positions_theory + [0., i*step_size_y] for i in range(len(ds_all))])[:,-1::-1]
	ds.scan_info.positions = positions
	ds.scan_info.positions_theory = positions

	ds.scan_info.data_filename = base_path + '/analysis/%s/%s_data_' % (scan_label, scan_label) + '%dx%d_dense.h5' % dpsize

	# little bug that needs to be solved7.18
	ds.mask = ds.fmask
	ds.save()
	
user = 'e14438'
dpsize = (192,192)
ctr=(306,263)#scans 1800 - 39XX   #(224,98)
detector_distance= 7.2122
first_scan = 1703
last_scan = 1711
#base_path = '/afs/psi.ch/user/b/braun_p/data'
base_path = '/home/philipp/Documents/master'
sy = 0.25e-6
sx = 0.02e-6
nx = 200
#dat_file = 'specES1_started_2013_03_16_1319.dat'
dat_file = 'specES1_started_2013_03_20_1108.dat'
#scans = [
#(1703,1711,20,62)]

size = 4e-6

scans.reverse()
for scan in scans:
	print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
	print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
	print 'Preparing scans %d_%d' % (scan[0],scan[1])
	print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
	print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
	prep_contdmesh_scan(base_path, user, dpsize, ctr, detector_distance, scan[0], scan[1], size/scan[3],size/(scan[3]+1),scan[3],dat_file)



